package com.ohs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ohs.mapper.admin.AdminMapper;
import com.ohs.pojo.Admin;
import com.ohs.pojo.Product;
import com.ohs.pojo.User;
import com.ohs.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired(required = false)
    private AdminMapper adminMapper;

    @Override
    public String doAdminLogin(Admin admin, Model model) {
        if (admin.getAdminName() == null || admin.getAdminName() == ""){
            model.addAttribute("msg", "用户名不能为空");
            return "admin/adminLogin";
        }
        if (admin.getAdminPassword() == null || admin.getAdminPassword() == ""){
            model.addAttribute("msg", "密码不能为空");
            return "admin/adminLogin";
        }
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("adminName", admin.getAdminName());
        Admin admin1= this.adminMapper.selectOne(adminQueryWrapper);
        if(admin1 == null){
            model.addAttribute("msg", "该管理员不存在");
            return "admin/adminLogin";
        }
        return "forward:/admin/selectAllGoodsByPage?currentPage=1";
    }

    @Override
    public String selectAllGoodsByPage(Integer currentPage, Model model) {
        PageHelper.startPage(currentPage, 8);
        List<Product> products = this.adminMapper.AdminFindAllProduct();
        PageInfo<Product> productPageInfo = new PageInfo<>(products);
        long total = productPageInfo.getTotal();
        int totalPage = (int)Math.ceil(total*1.0 / 8);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("PageInfo", productPageInfo.getList());
        return "admin/selectGoods";
    }

    @Override
    public String addProduct(Product product, MultipartFile fileName, HttpSession session) {
        if (fileName == null){
            return "redirect:admin/toAddProductPage";
        }
        try {
            String originalFilename = fileName.getOriginalFilename();
            int index = originalFilename.indexOf(".");
            String suffix = originalFilename.substring(index);
            long time = System.currentTimeMillis();
            String newFile = time + suffix;
            //String path = session.getServletContext().getRealPath("upload");
            String path = "D:\\ideaspacework\\furniture-pro\\src\\main\\resources\\static\\img\\";
            File file = new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            product.setProPhoto(newFile);
            int row = this.adminMapper.addProduct(product);
            if (row != 1){
                return "redirect:admin/toAddProductPage";
            }
            String path1 = path + newFile;
            File file1 = new File(path1);
            fileName.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "forward:/admin/selectAllGoodsByPage?currentPage=1";
    }

    @Override
    public String touUpdateProductPage(Integer proId, Model model) {
        Product product = this.adminMapper.findProductByProId(proId);
        model.addAttribute("product", product);
        return "admin/updateProduct";
    }

    @Override
    public String updateProduct(Product product) {
        int row = this.adminMapper.updateProductByProId(product);
        if (row != 1){
            return "redirect:admin/touUpdateProductPage";
        }
        return "forward:/admin/selectAllGoodsByPage?currentPage=1";
    }

    @Override
    public String doDeleteProductPage(Integer proId) {
         int row=this.adminMapper.deleteProductByProId(proId);
         if(row!=1){
             return "redirect:/admin/updateProduct";
         }
        return "forward:/admin/selectAllGoodsByPage?currentPage=1";
    }

    @Override
    public String findAllUsers(Integer currentPage, Model model) {
        PageHelper.startPage(currentPage, 8);
        List<User> users = this.adminMapper.findAllUsers();
        System.out.println(users);
        PageInfo<User> productPageInfo = new PageInfo<>(users);
        long total = productPageInfo.getTotal();
        int totalPage = (int)Math.ceil(total*1.0 / 8);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("PageInfo", productPageInfo.getList());
        return "admin/allUsers";
    }

}
